package com.demo.cn.pretest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ActivityWebviewActivity extends Activity {


    @InjectView(R.id.webview)
    WebView webview;
    @InjectView(R.id.ed_tv)
    EditText edTv;
    @InjectView(R.id.btn)
    Button btn;
    @InjectView(R.id.tv)
    TextView tv;
    private long currentTimeMillisstart, currentTimeMillisend;
    private long time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.inject(this);
        initWebView();
        initView();

    }

    private void initWebView() {
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                currentTimeMillisstart = System.currentTimeMillis();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                currentTimeMillisend = System.currentTimeMillis();
                time = currentTimeMillisend - currentTimeMillisstart;
                tv.setText(time+" ms");

            }
        });


    }

    private void initView() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webview.loadUrl(edTv.getText().toString().trim());
                currentTimeMillisstart = System.currentTimeMillis();
            }
        });
    }

}
